import createRouteMap from './create-route-map'
import { createRoute } from './history/base'

export default function createMatcher(routes) {
  // 路由表进行扁平化处理
  // pathMap = {'/': Home, '/about': About, '/about/a', 'aboutA'}
  let { pathMap } = createRouteMap(routes) // 扁平化配置
  console.log('pathMap', routes)
  function addRoutes(routes) {
    createRouteMap(routes, routes)
  }
  function match(location) {
    let record = pathMap[location]
    console.log('record', record)
    if (record) {
      return createRoute(record, {
        path: location
      })
    }
    // 记录可能没有
    return createRoute(null, {
      path: location
    })

    // 可能一个记录对用多个记录
  }
  return {
    addRoutes, // 动态添加路由
    match // 用于匹配路径
  }
}
